package com.sh.data.engine.repository.dao.common;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sh.data.engine.repository.dao.common.entity.CommonConfigEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author jingchen.hr
 */
@Repository
public interface CommonConfigMapper extends BaseMapper<CommonConfigEntity> {

    /**
     * 通过configKey查询配置
     *
     * @param configKey
     * @return
     */
    default CommonConfigEntity queryByConfigKey(String configKey) {

        LambdaQueryWrapper<CommonConfigEntity> query = new QueryWrapper<CommonConfigEntity>().lambda();
        query
            .eq(CommonConfigEntity::getRowState, 1)
            .eq(CommonConfigEntity::getConfigKey, configKey)
            .orderByDesc(CommonConfigEntity::getId);
        List<CommonConfigEntity> list = this.selectList(query);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }
}
